<template>
  <div class="home-container">
    <!-- 头部搜索按钮 -->
    <van-nav-bar class="page-nav-bar">
      <van-button
        slot="title"
        class="search-btn"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- 中间频道tabs列表 -->
    <van-tabs class="channel-tabs" v-model="tabIndex" animated swipeable>
      <!-- 循环频道数组，生成频道tab -->
      <van-tab v-for="s in channels" :key="s.id" :title="s.name">
        <!-- 频道内容 -->
        <ArticleList :channel="s" />
      </van-tab>

      <!-- 汉堡按钮 -->
      <div slot="nav-right" class="hamburger-btn" @click="isShowEdit = true">
        <i class="iconfont icon-fenlei"></i>
      </div>
    </van-tabs>
    <!-- 频道编辑弹层 -->
    <van-popup
      v-model="isShowEdit"
      position="bottom"
      closeable
      close-icon-position="top-left"
      :style="{ height: '100%' }"
    >
      <ChannelEdit @changeTab="changeTabIndex" :usrChanList="channels" :index="tabIndex"></ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from "@/api/user.js";
import ArticleList from "./components/article-list.vue";
import ChannelEdit from "./components/channel-edit.vue";
import { getItem } from "@/utils/storage.js";
export default {
  name: "home",
  props: {},
  data() {
    return {
      isShowEdit: false,
      tabIndex: 0,
      channels: [],
    };
  },
  async created() {
    try {
      // 1.从本地获取用户频道数组
      const localUsrChanList = getItem(this.$conf.USER_CHAN);
      if (localUsrChanList && !this.$store.state.user) {
        this.channels = localUsrChanList;
      } else {
        const { data: res } = await getUserChannels();
        // console.log("频道列表", res.data.channels);
        this.channels = res.data.channels;
      }
    } catch (error) {
      this.$toast.fail("请求频道列表失败");
      console.log("出错了：", error.message);
    }
    //   // 如果登录
    //   if (this.$store.state.user) {
    //     // 请求获取频道列表接口
    //     const { data: res } = await getUserChannels();
    //     console.log("频道列表", res.data.channels);
    //     this.channels = res.data.channels;
    //   } else {
    //     // 如果未登录
    //     const localUsrChanList = getItem(this.$conf.USER_CHAN);
    //     if (localUsrChanList) {
    //       this.channels = localUsrChanList;
    //     } else {
    //       const { data: res } = await getUserChannels();
    //       this.channels = res.data.channels;
    //     }
    //   }
    // } catch (error) {
    //   this.$toast.fail("请求频道列表失败");
    // }
  },
  methods: {
    // 子组件传递被选中频道下标
    changeTabIndex(i) {
      this.tabIndex = i
      this.isShowEdit=false
    }
  },
  computed: {},
  watch: {},
  components: { ArticleList, ChannelEdit },
};
</script>


<style lang="less" scoped>
.home-container {
  // 1.标题栏---------------------------------
  /deep/ .van-nav-bar__content {
    position: fixed;
    background-color: #0094ff;
    width: 750px;
  }
  /deep/ .van-nav-bar__title {
    max-width: unset !important;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
      color: #fff;
    }
  }
  // 2.tab栏 ---------------------------------
  /deep/ .channel-tabs {
    padding-top: 180px;
    padding-bottom: 100px;
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
      color: #777;
    }

    .van-tab--active {
      color: #333;
    }
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 82px;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }

    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }
  }
  // 3.汉堡按钮 ---------------------------------
  // 3.1 汉堡按钮的占位符
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }
  // 3.2 汉堡按钮样式
  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    background-color: rgba(255, 255, 255, 0.902);
    i.iconfont {
      font-size: 33px;
    }
    &:before {
      content: "";
      position: absolute;
      left: 0;
      width: 1px;
      height: 58px;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
}
</style>